/////////////////////////////////////////////////////////////
// File Name		: "MainMenuState.cpp"
// 
// Author Name		:  Cristhian De La Paz
//
// Purpose			: 
/////////////////////////////////////////////////////////////
#include "MainMenuState.h"
#include "CGamePlayState.h"
#include "Game.h"

CMainMenuState::CMainMenuState()
{
	m_nMenuCursorID = -1;
	m_nBitmapFontID = -1;
	m_nBackGroundID = -1;
	m_nMenuMusicID	= -1;
	m_nOverSoundID	= -1;
	m_nSelectSoundID= -1;
	m_pDInput = NULL;
	
	m_tItemArray[0].width = 128;
	m_tItemArray[1].width = 224;
	m_tItemArray[2].width = 352;
	m_tItemArray[3].width = 352;
	m_tItemArray[4].width = 224;
	m_tItemArray[5].width = 128;
}

CMainMenuState* CMainMenuState::GetInstance()
{
	static CMainMenuState instance;
	return &instance;
}

void CMainMenuState::Enter( void )
{
	currentSelection = 0;
	m_pDInput = CSGD_DirectInput::GetInstance();
	m_nMenuCursorID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/SGD_MenuCursor.png");
	m_nBitmapFontID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/fonts/AAFont3.png");
	m_nBackGroundID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/menus/MainMenu.png");
	m_nMenuMusicID	= CSGD_FModManager::GetInstance()->LoadSound("resource/sound/menu_music.ogg", FMOD_LOOP_NORMAL);
	m_nOverSoundID	= CSGD_FModManager::GetInstance()->LoadSound("resource/sound/Menu/over.wav");
	m_nSelectSoundID= CSGD_FModManager::GetInstance()->LoadSound("resource/sound/Menu/select.wav");
	m_bitmapFont.SetBitmapInformation(32,32,10,' ',m_nBitmapFontID);

	int startPosX = m_tItemArray[currentSelection].width/2 + 384;

	m_tItemArray[0].posX = startPosX;

	for(int i = 1; i<6; ++i)
	{
		m_tItemArray[i].posX = m_tItemArray[i-1].posX + m_tItemArray[i].width + 64;
	}

	CSGD_FModManager::GetInstance()->PlaySoundA(m_nMenuMusicID);
}

bool CMainMenuState::Input( void )
{
	m_pDInput->ReadDevices();

	if (m_pDInput->KeyPressed(DIK_RIGHT))
	{
		currentSelection++;
		if(currentSelection > 5)
			currentSelection = 5;

		CSGD_FModManager::GetInstance()->PlaySoundA(m_nOverSoundID);
	}

	if (m_pDInput->KeyPressed(DIK_LEFT))
	{
		currentSelection--;
		if(currentSelection < 0)
			currentSelection = 0;

		CSGD_FModManager::GetInstance()->PlaySoundA(m_nOverSoundID);
	}

	if (m_pDInput->KeyPressed(DIK_RETURN))
	{
		CSGD_FModManager::GetInstance()->PlaySoundA(m_nSelectSoundID);

		if(currentSelection == 0)
		{
			CGame::GetInstance()->ChangeState(CGamePlayState::GetInstance());
		}
		else
		{
			return false;
		}
	}

	return true;
}

void CMainMenuState::Update( void )
{
	//Center the current menu selection
	//384 center of screen
	//itemwidth[i]/2

	m_tItemArray[currentSelection].posX = 40/*m_tItemArray[currentSelection].width + 384*/;
	
	for(int i = currentSelection-1; i>=0; --i)
	{
		m_tItemArray[i].posX = m_tItemArray[i+1].posX - m_tItemArray[i+1].width - 512;
	}

	for(int i = currentSelection+1; i<6; ++i)
	{
		m_tItemArray[i].posX = m_tItemArray[i-1].posX + m_tItemArray[i-1].width + 64;
	}
}

void CMainMenuState::Render( void )
{
	CSGD_TextureManager::GetInstance()->Draw(m_nBackGroundID, 0, 0, 1.0f, 0.8f, NULL);	

	m_bitmapFont.Draw("PLAY",			m_tItemArray[0].posX,173,1.0f,D3DCOLOR_XRGB(255,255,255));			// 4 chars = 192
	m_bitmapFont.Draw("OPTIONS",		m_tItemArray[1].posX,173,1.0f,D3DCOLOR_XRGB(255,255,255));		// 7 chars = 288
	m_bitmapFont.Draw("HOW TO PLAY",	m_tItemArray[2].posX,173,1.0f,D3DCOLOR_XRGB(255,255,255));	// 11 chars= 416
	m_bitmapFont.Draw("HIGH SCORES",	m_tItemArray[3].posX,173,1.0f,D3DCOLOR_XRGB(255,255,255));	// 11 chars= 416
	m_bitmapFont.Draw("CREDITS",		m_tItemArray[4].posX,173,1.0f,D3DCOLOR_XRGB(255,255,255));		// 7 chars = 288
	m_bitmapFont.Draw("EXIT",			m_tItemArray[5].posX,173,1.0f,D3DCOLOR_XRGB(255,255,255));			

	//CSGD_TextureManager::GetInstance()->Draw(m_nMenuCursorID, 220, cursorYPos, 1.0f, 1.0f, NULL);	
}

void CMainMenuState::Exit( void )
{
	CSGD_FModManager::GetInstance()->StopSound(m_nMenuMusicID);

	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nMenuCursorID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBackGroundID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBitmapFontID);
	m_nMenuCursorID = -1;
	m_nBitmapFontID = -1;
	m_nBackGroundID = -1;
	m_pDInput = NULL;
}

CMainMenuState::~CMainMenuState()
{

}

